package com.example.yichuangyun.dialog;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;

import com.example.yichuangyun.R;
import com.example.yichuangyun.databinding.PopupLayoutBinding;
import com.example.yichuangyun.model.business.SortModel;
import com.example.yichuangyun.ui.adapter.EmptyRecyclerCommonAdapter;
import com.example.yichuangyun.ui.adapter.RecyclerViewHolder;

import java.util.ArrayList;
import java.util.List;

public abstract class SortPopupWindow extends PopupWindow {

    private final PopupWindow popupWindow;
    private final Context context;
    private final List<SortModel> data = new ArrayList<>();
    private final EmptyRecyclerCommonAdapter<SortModel> adapter;
    private int businessType = 2;
    private SortModel sortModel;

    public SortPopupWindow(Context context, boolean isBusiness) {
        PopupLayoutBinding binding = PopupLayoutBinding.inflate(LayoutInflater.from(context));
        popupWindow = new PopupWindow(binding.getRoot(), ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setContentView(binding.getRoot());
        popupWindow.setTouchable(true);
        this.context = context;
        data.add(new SortModel(context.getString(R.string.weight), false, isBusiness ? "chanceWeight" : "contactWeight", ""));
        data.add(new SortModel(context.getString(R.string.create_time), false, "createTime", ""));
        data.add(new SortModel(context.getString(R.string.follow_time), false, "lastFollowTime", ""));
        if (isBusiness) {
            data.add(new SortModel(context.getString(R.string.into_time_text), false, "intoChanceTime", ""));
        }
        adapter = new EmptyRecyclerCommonAdapter<SortModel>(context, R.layout.item_select_sort, data) {
            @Override
            public void convert(RecyclerViewHolder holder, SortModel item, int position) {
                ImageView ivSort = holder.getView(R.id.iv_sort);
                holder.setViewVisibility(R.id.iv_sort, item.isSelect() ? View.VISIBLE : View.GONE);
                ivSort.setImageResource(item.getSortType().equals("desc") ? R.mipmap.icon_jiangxu : R.mipmap.icon_shengxu);
                holder.setText(R.id.tv_title, item.getLabelName());
            }
        };
        adapter.setOnItemClickListener((parent, view1, position) -> {
            SortModel mModel = data.get(position);
            if (mModel.isSelect()) {
                mModel.setSortType(mModel.getSortType().equals("desc") ? "asc" : "desc");
            } else {
                mModel.setSortType("desc");
            }
            for (SortModel sortModel : data) {
                sortModel.setSelect(false);
            }
            mModel.setSelect(true);
            sortModel = mModel;
            adapter.notifyDataSetChanged();
        });
        binding.recyclerView.setAdapter(adapter);
        binding.view.setOnClickListener(v -> popupWindow.dismiss());
        binding.tvReset.setOnClickListener(v -> {
            for (SortModel sortModel : data) {
                sortModel.setSelect(false);
            }
            if (isBusiness) {
                setSort(businessType);
            } else {
                resetSort();
            }
            adapter.notifyDataSetChanged();
        });
        binding.tvConfirm.setOnClickListener(v -> {
            if (sortModel != null) {
                confirm(sortModel.getSortType(), sortModel.getOrderKey());
            } else {
                confirm("", "");
            }
            popupWindow.dismiss();
        });
    }

    public void setSort(int type) {
        businessType = type;
        // 0-我创建的、1-部门创建的、 2-我跟进的、 3-部门跟进的、4-共享给我的、5-共享给部门的
        if (type == 0 || type == 1) {
            for (SortModel model : data) {
                if (model.getLabelName().equals(context.getString(R.string.create_time))) {
                    model.setSelect(true);
                    model.setSortType("desc");
                    sortModel = model;
                } else {
                    model.setSelect(false);
                }
            }
        } else {
            for (SortModel model : data) {
                model.setSelect(false);
            }
            sortModel = null;
        }
        adapter.notifyDataSetChanged();
    }

    public void resetSort() {
        for (SortModel model : data) {
            model.setSelect(false);
        }
        sortModel = null;
        adapter.notifyDataSetChanged();
    }

    public void showPopupWindow(View view) {
        if (!popupWindow.isShowing()) {
            int offsetX = view.getWidth() - popupWindow.getContentView().getMeasuredWidth();
            int offsetY = 0;
            popupWindow.showAsDropDown(view, offsetX, offsetY, Gravity.START);
        }
    }

    public abstract void confirm(String sortType, String orderKey);
}
